home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / minilint.c < prev    next >
Text File  |  1985-06-03  |  3KB  |  92 lines

  1. /****************************************************************
  2. *                                *
  3. *            CC (C Checker)                *
  4. *                                *
  5. *         C Source Paren, bracket and comment Checker     *
  6. *                                *
  7. *             T. Jennings  -- Sometime in 1983        *
  8. *        Slightly tweaked and renamed MINILINT        *
  9. *            KAB Aug 84                *
  10. *                                *
  11. *                                *
  12. ****************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16.  
  17. /* Very crude but very effective C source debugger. Counts the numbers of
  18. matching braces, parenthesis and comments, and displays them at the left edge
  19. of the screen. The best way to see what it does is to do it; try
  20.  
  21.     MINILINT MINILINT.C        /* C Check MINILINT.C */
  22.  
  23. Properly handles parens and brackets inside comments; they are ignored. 
  24. */
  25.  
  26. main(argc,argv)
  27.     int argc;
  28.     char **argv;
  29. {
  30. int file;    
  31. char c,lastc;
  32. int parens,brackets,comments;
  33. int line, col;
  34. char hdr[40];
  35.  
  36.     file= open(argv[1],0x8000);
  37.     if (file == -1) {
  38.         cputs("File missing.\r\nTry MINILINT <filename.ext> \r\n");
  39.         exit();
  40.     }
  41.     brackets= parens= comments= 0;
  42.     line= 0; col= 0;
  43.     lastc= '\0';
  44.  
  45.     while (read(file,&c,1)) {
  46.         if (c == 0x1a) break;            /* stop if ^Z */
  47.  
  48.         if (col == 0) {
  49.             sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
  50.             while (strlen(hdr) < 23)    /* gross but works */
  51.                 strcat(hdr," ");    /* to hell with elegant, */
  52.             cprintf("%s|",hdr);        /* we got a job to do !*/
  53.         }                /* (real programmers dont ...) */
  54.  
  55. /* Dont count parens and brackets that are inside comments. This of course
  56. assumes that comments are properly matched; in any case, that will be the
  57. first thing to look for. */
  58.  
  59.         if (comments <= 0) {
  60.             if (c == '{') ++brackets;
  61.             if (c == '(') ++parens;
  62.             if (c == '}') --brackets;
  63.             if (c == ')') --parens;
  64.         }
  65.  
  66. /* Now do comments. This properly handles nested comments;
  67. whether or not the compiler does is your responsibility */
  68.  
  69.         if ((c == '*') && (lastc == '/')) ++comments;
  70.         if ((c == '/') && (lastc == '*')) --comments;
  71.  
  72.         ++col;
  73.         if (c == 0x0a) {        /* newline == New Line */
  74.             col= 0;            /* set column 0 */
  75.             ++line;
  76.         }
  77.         putchar(c);            /* display text */
  78.         lastc= c;            /* update last char */
  79.     }
  80.     cputs("\r\n\r\n");
  81.     if (brackets) cputs("Unbalanced brackets\r\n");
  82.     if (parens) cputs("Unbalanced parenthesis\r\n");
  83.     if (comments) cputs("Unbalanced comments\r\n");
  84. }
  85.  
  86. /* eof */*/
  87.  
  88. #include <stdio.h>
  89. #include <ctype.h>
  90.  
  91. /* Very crude but very effective C source debugger. Counts the numbers of
  92. match